package cn.xdf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/demo05")
public class Demo05Controller_SessionAttribute {

    @GetMapping("/demo01")
    public String demo01(HttpSession session)throws IOException{

        session.setAttribute("loginUser","zhangsan");

        return "redirect:/demo05/demo02";
    }
    
    /**
     * 从session中取出loginUser(必须要有这个属性)
     * @param loginUser
     * @param response
     * @throws IOException
     */
    @GetMapping("/demo02")
    public void demo02(@SessionAttribute String loginUser,
                       HttpServletResponse response)throws IOException{
        response.setContentType("text/html;charset=utf8");
        response.getWriter().write("loginUser: "+loginUser+"<hr/>");

    }

}
